#ifndef OBSAPP/VIEWS.PY
#define OBSAPP/VIEWS.PY
# Create your views here.

from django.http import HttpResponseRedirect

from django.shortcuts import render_to_response
from forms import LoginForm,UpdateForm
from django.contrib.auth import authenticate,login as auth_login
from django.contrib.auth import logout as user_logout
from django.contrib.auth.models import User
from models import *
from django.core.urlresolvers import reverse
import datetime

def transkriptGoruntule(request):
    notlar={'AA':4,'BA':3.5,'BB':3,'CB':2.5,'CC':2,'DC':1.5,'DD':1,'FD':0.5,'FF':0,'DS':0,'YE':0,'YS':0}
    
    ogrenci=Ogrenci.objects.get(kimlikBilgisi=KimlikBilgileri.objects.get(kullanici=request.user.id))
    transkript=Transkript.objects.filter(ogrenciNo=ogrenci)
    gno=0
    toplamkredi=0
    if transkript:
        for tr in transkript:
            toplamkredi=toplamkredi+tr.dersKredisi
            gno=gno+notlar[tr.dersNotu]*tr.dersKredisi
        print toplamkredi
        gno=gno*1.0/toplamkredi
    kimlikbil=KimlikBilgileri.objects.get(kullanici=request.user)
    return render_to_response("ogrenci/transkript.html",{'ogrenci':request.user,'ogrencibil':ogrenci,'transkript':transkript,'kimlikbilgisi':kimlikbil,'gno':gno})

def takenCourse(request):
    kimlikbil=KimlikBilgileri.objects.get(kullanici=request.user)
    ogrencibil=Ogrenci.objects.get(kimlikBilgisi=request.user)
    alinanDersler=AldigiDersler.objects.filter(ogrenciNo=ogrencibil)
    return render_to_response('ogrenci/takencourse.html',{'ogrenci':request.user,'ogrbilgisi':ogrencibil,'alinandersler':alinanDersler})

def saveCourse(request):
    print "a"
    kimlikbil=KimlikBilgileri.objects.get(kullanici=request.user)
    ogrencibil=Ogrenci.objects.get(kimlikBilgisi=request.user)
    if request.method=="POST":
        AldigiDersler.objects.filter(ogrenciNo=ogrencibil).delete()

        secilendersler=request.POST.getlist('acilanders')
        for ders in secilendersler:
            d=Ders.objects.get(id=ders)
            AldigiDersler.objects.create(ogrenciNo=ogrencibil,dersKodu=d,alinanYil='2011-12-12',aldigiNot="00",devamsizlik=0,durum=0)
        return render_to_response("ogrenci/index.html",{'ogrenci':request.user,'ogrbilgisi':ogrencibil})

def courseRecord(request):
    kimlikbil=KimlikBilgileri.objects.get(kullanici=request.user)
    ogrencibil=Ogrenci.objects.get(kimlikBilgisi=request.user)
    acilanders=AcilanDers.objects.filter(sinif=ogrencibil.sinif)
    alinanders=AldigiDersler.objects.filter(ogrenciNo=ogrencibil)
    alinanders = [x.dersKodu.id for x in alinanders]
    print alinanders 
    for ders in acilanders:
        print ders
    #print acilanders[1].dersKodu.dersKodu
    return render_to_response('ogrenci/courserecordlist.html',{'ogrenci':request.user,'ogrbilgisi':ogrencibil,'acilandersler':acilanders,'aldigidersler':alinanders})

def update(request):
    if request.method=="POST":
        form=UpdateForm(request.POST)
        if form.is_valid():
            email=form.cleaned_data['email']
            adres1=form.cleaned_data['adres1']
            adres2=form.cleaned_data['adres2']
            adres3=form.cleaned_data['adres3']
            websayfasi=form.cleaned_data['websayfasi']
            ogrenci=User.objects.get(username=request.user.username)
            kimlikbil=KimlikBilgileri.objects.get(kullanici=ogrenci)
            kimlikbil.adres1=adres1
            kimlikbil.adres2=adres2
            kimlikbil.adres3=adres3
            kimlikbil.email=email
            kimlikbil.websayfasi=websayfasi
            kimlikbil.save()
    kimlikbil=KimlikBilgileri.objects.get(kullanici=request.user)
    ogrencibil=Ogrenci.objects.get(kimlikBilgisi=request.user)
    bolum=BolumKayit.objects.get(ogrenciNo=ogrencibil.id)
    bolum= bolum.bolum.bolumAdi
    data={'email':request.user.email,'adres1':kimlikbil.adres1,'adres2':kimlikbil.adres2,'adres3':kimlikbil.adres3,'websayfasi':kimlikbil.websayfasi}
    form = UpdateForm(initial=data)
    return render_to_response('ogrenci/update.html',{'form':form,'ogrenci':request.user,'ogrbilgisi':ogrencibil,'bolum':bolum})


def index(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("/login")
    
    #if request.user.usernamee[0]
    kimlik= KimlikBilgileri.objects.get(kullanici=request.user)
    print kimlik.adres1
    try:
        ogrencibil=Ogrenci.objects.get(kimlikBilgisi=request.user)
    except Ogrenci.DoesNotExist:
        return HttpResponseRedirect("/login")
    print ogrencibil.sinif
    return render_to_response("ogrenci/index.html",{'ogrenci':request.user,'ogrbilgisi':ogrencibil})


def logout(request):
    user_logout(request)
    return HttpResponseRedirect(reverse("loginuser"))

def login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse("index"))
    if request.method=="POST":
        form=LoginForm(request.POST)
        if form.is_valid():
            usernamee = form.cleaned_data['usernamee']
            passwordd = form.cleaned_data['passwordd']
            ogrenci = authenticate(username=usernamee,password=passwordd)
            if ogrenci is not None:
                auth_login(request,ogrenci)
            else:
                return render_to_response('ogrenci/login.html',{'title':'Login', 'form':LoginForm()})
            print usernamee
            if usernamee[0]=='5':
                print "aaaaaaaaaaaaa"
                return HttpResponseRedirect('/gorevli')
            return HttpResponseRedirect("/")
        else:
            return render_to_response('ogrenci/login.html',{'title':'Login', 'form':LoginForm()})
    else:
        return render_to_response('ogrenci/login.html',{'title':'Login', 'form':LoginForm()})
